﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GesturePlugin
{
    public partial class GesturePluginUserEditor : UserControl
    {
        private string _gestureString = "";
        private GestureEditor _gestureEditor;
        private ListView _gesturesInDictionnary;

        public GesturePluginUserEditor(GestureEditor e)
        {
            InitializeComponent();
            _gestureEditor = e;
            DefaultPrecisionCapture.Value = _gestureEditor.DefaultPrecision;
            CreateMyListView();
        }

        private void CreateMyListView()
        {
            // Create a new ListView control.
            _gesturesInDictionnary = new ListView();
            _gesturesInDictionnary.Bounds = new Rectangle(new Point(10, 15), new Size(280, 350));

            // Set the view to show details.
            _gesturesInDictionnary.View = View.Details;
            // Select the item and subitems when selection is made.
            _gesturesInDictionnary.FullRowSelect = true;
            // Display grid lines.
            _gesturesInDictionnary.GridLines = true;
            // Sort the items in the list in ascending order.
            _gesturesInDictionnary.Sorting = SortOrder.Ascending;

            // Create three items and three sets of subitems for each item.
            IList currentGestureDictionnary = _gestureEditor.GestureDictionnary;

            for (int i = 0; i < currentGestureDictionnary.Count; i++)
            {
                Gesture gesture = (Gesture)currentGestureDictionnary[i];

                for (int j = 0; j < gesture.GesturePatterns.Count; j++)
                {
                    ListViewItem item = new ListViewItem(gesture.Name);
                    string pattern = (string) gesture.GesturePatterns[j];
                    item.SubItems.Add(pattern);
                    _gesturesInDictionnary.Items.Add(item);
                }
            }

            // Create columns for the items and subitems.
            // Width of -2 indicates auto-size.
            _gesturesInDictionnary.Columns.Add("Nom de la gesture", -2, HorizontalAlignment.Left);
            _gesturesInDictionnary.Columns.Add("Pattern de la gesture", -2, HorizontalAlignment.Left);

            // Add the ListView to the control collection.
            gestureDictionnary.Controls.Add(_gesturesInDictionnary);
        }


        private void defaultPrecisionCapture_ValueChanged(object sender, EventArgs e)
        {
            _gestureEditor.DefaultPrecision = (int) DefaultPrecisionCapture.Value;
        }

        private void buttonRight_Click(object sender, EventArgs e)
        {
            addCharToGestureString("1");
        }

        private void buttonUpRight_Click(object sender, EventArgs e)
        {
            addCharToGestureString("2");
        }

        private void buttonUp_Click(object sender, EventArgs e)
        {
            addCharToGestureString("3");
        }

        private void buttonUpLeft_Click(object sender, EventArgs e)
        {
            addCharToGestureString("4");
        }

        private void buttonLeft_Click(object sender, EventArgs e)
        {
            addCharToGestureString("5");
        }

        private void buttonDownLeft_Click(object sender, EventArgs e)
        {
            addCharToGestureString("6");
        }

        private void buttonDown_Click(object sender, EventArgs e)
        {
            addCharToGestureString("7");
        }

        private void buttonDownRight_Click(object sender, EventArgs e)
        {
            addCharToGestureString("8");
        }

        private void buttonErase_Click(object sender, EventArgs e)
        {
            if(_gestureString.Length > 0)
            {
                _gestureString = _gestureString.Remove(_gestureString.Length - 1);
                labelGestureString.Text = _gestureString;
            }

        }

        private void buttonSaveGesture_Click(object sender, EventArgs e)
        {
            string gestureName = textBoxGestureName.Text;
            if (!gestureName.Equals("") || !_gestureString.Equals(""))
            {
                _gestureEditor.AddNewGesture(gestureName, _gestureString);
                ListViewItem item = new ListViewItem(gestureName);
                item.SubItems.Add(_gestureString);
                _gesturesInDictionnary.Items.Add(item);
            }
        }

        private void addCharToGestureString(string s)
        {
            if (_gestureString.Length == 0)
            {
                _gestureString += s;
                labelGestureString.Text = _gestureString;
            }
            else if (_gestureString[_gestureString.Length - 1].ToString() !=  s)
            {
                _gestureString += s;
                labelGestureString.Text = _gestureString;
            }
        }

    }
}
